package com.vitabuybuy.tv

import android.app.ActivityManager
import android.content.Context
import android.os.Environment
import android.os.Process
import android.text.style.BulletSpan
import android.util.Log
import android.widget.Toast
import cn.jpush.android.api.JPushInterface
import com.effective.android.anchors.Project
import com.effective.android.anchors.Task
import com.effective.android.anchors.TaskCreator
import com.library.common.base.appContext
import com.library.common.listener.KtxActivityLifecycleCallbacks
import com.library.common.net.HeadInterceptor
import com.library.common.net.NetHttpClient
import com.library.common.net.interception.LogInterceptor
import com.library.common.toast.IToastInterceptor
import com.library.common.toast.ToastInterceptor
import com.library.common.toast.ToastUtils
import com.library.common.util.XLog
import com.tencent.mmkv.MMKV
import com.tencent.qcloud.tim.uikit.TUIKit
import com.tencent.qcloud.tim.uikit.config.GeneralConfig
import com.tencent.qcloud.tim.uikit.config.TUIKitConfigs
import com.tencent.rtmp.TXLiveBase
import com.vitabuybuy.tv.ui.tencent.chat.ZxyTimUtls
import me.jessyan.autosize.AutoSize
import me.jessyan.autosize.AutoSizeConfig
import rxhttp.wrapper.param.RxHttp
import java.io.File

/**
 * 作者　: SimGa
 * 时间　: 2020/11/17
 * 描述　:
 */
object TaskCreator : TaskCreator {
    override fun createTask(taskName: String): Task {
        return when (taskName) {
            InitNetWork.TASK_ID -> InitNetWork()
            InitToast.TASK_ID -> InitToast()
            InitAppLifecycle.TASK_ID -> InitAppLifecycle()
            InitAutoSize.TASK_ID -> InitAutoSize()
            else -> InitDefault()
        }
    }
}

class InitDefault : Task(TASK_ID, true) {
    companion object {
        const val TASK_ID = "0"
    }

    override fun run(name: String) {
        //初始化Log打印
        XLog.init(BuildConfig.DEBUG)
        //初始化MMKV
        MMKV.initialize(appContext.filesDir.absolutePath)
    }
}

//初始化网络
class InitNetWork : Task(TASK_ID, true) {
    companion object {
        const val TASK_ID = "1"
    }

    override fun run(name: String) {
        //传入自己的OKHttpClient 并添加了自己的拦截器
        RxHttp.init(NetHttpClient.getDefaultOkHttpClient().run {
            addInterceptor(HeadInterceptor())//自定义头部拦截器
            addInterceptor(LogInterceptor())//添加Log拦截器
        }.build())
        //初始化SDK
        doJob()
    }
}

//初始化Utils
class InitToast : Task(TASK_ID, false) {
    companion object {
        const val TASK_ID = "2"
    }

    override fun run(name: String) {
        //初始化吐司 这个吐司必须要主线程中初始化
        ToastUtils.setToastInterceptor(object : ToastInterceptor(), IToastInterceptor {
            override fun intercept(toast: Toast?, text: CharSequence?): Boolean {
                val intercept = super.intercept(toast, text)
                if (intercept) {
                    Log.e("Toast", "空 Toast")
                } else {
                    Log.i("Toast", text.toString())
                }
                return intercept
            }
        })
        // 初始化吐司工具类
        ToastUtils.init(appContext)
    }
}

//初始化Utils
class InitAppLifecycle : Task(TASK_ID, true) {
    companion object {
        const val TASK_ID = "3"
    }

    override fun run(name: String) {
        //注册全局的Activity生命周期管理
        appContext.registerActivityLifecycleCallbacks(KtxActivityLifecycleCallbacks())
    }
}

class InitAutoSize : Task(TASK_ID, false) {
    companion object {
        const val TASK_ID = "4"
    }

    override fun run(name: String) {
        AutoSize.initCompatMultiProcess(appContext)
        AutoSizeConfig.getInstance()
            .setCustomFragment(true)
            .setExcludeFontScale(true)
            .isUseDeviceSize = true
    }
}

class AppTaskFactory : Project.TaskFactory(TaskCreator)

/**
 * 初始化SDK
 * @param millis Long
 */
fun doJob() {
    //初始化极光SDK
    JPushInterface.setDebugMode(true)
    JPushInterface.init(appContext)
    //初始化腾讯云直播
    TXLiveBase.getInstance().setLicence(appContext, BuildConfig.LIVELICENSEURL, BuildConfig.LIVEKEY)
    ZxyTimUtls.getInstance().initTIM(appContext, BuildConfig.LIVEAPPID)
}

private fun isMainProcess(context: Context): Boolean {
    val pid = Process.myPid()
    val activityManager =
        context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
    for (appProcess in activityManager.runningAppProcesses) {
        if (appProcess.pid == pid) {
            return context.applicationInfo.packageName == appProcess.processName
        }
    }
    return false
}